package com.mall.exception;

import com.mall.enums.CodeNumEntity;
import com.mall.pojo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description:
 * @author: liang
 * @time: 2024-01-17 10:43
 */
@ControllerAdvice
@Slf4j
public class GlobalException {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseMessage serviceExceptionHandler(Exception e) {
        log.error("出现未知错误:{}",e.getMessage());
        //未知异常处理
        return new ResponseMessage(CodeNumEntity.UNKNOWN_ERROR.getCode(), CodeNumEntity.UNKNOWN_ERROR.getMessage() + "::"+e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResponseMessage serviceExceptionHandler(ServiceException e) {//服务器异常处理
        return new ResponseMessage(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(FileUploadException.class)
    @ResponseBody
    public ResponseMessage FileUploadExceptionHandler(FileUploadException e) {//文件上传异常
        return new ResponseMessage(e.getCode(), e.getMessage());
    }
}
